exports.MMMallModel = Core.Class.subclass(
{
	classname:'MMMallModel',
								   
    initialize: function(mallModel) 
    {
    	this._mallModel = null;
    	this._allFloors = null;
    	this._allStores = null;
    	this._allElevators = null;
    	
    	if(mallModel) 
    	{
    		this._mallModel = mallModel;
    	} 
    	else 
    	{
    		this._mallModel = {"mallId": 1, "mallName": "My New Mall", "elevatorId" : 2, "floorId": 1, "storeId" : 1, "lastTrashCollectedTime": 0, "allFloors": {}, "allStores" : {}, "allElevators" : {} };
    	}
    	
    	this._allFloors = this.initializeAllFloors();
    	this._allStores = this.initializeAllStores();
    	this._allElevators = this.initializeAllElevators();
    },
    
	getName: function()
    {
    	return this._mallModel.mallName;
    },
    
    
	setName: function(mallName)
    {
    	this._mallModel.mallName = mallName;
    },
    
   	getFloorCount:function()
   	{
   		return this._allFloors.getLength();
   	},
    
	getLastTrashCollectedTime: function() 
    {
    	return this._mallModel.lastTrashCollectedTime;
    },

    setLastTrashCollectedTime: function(lastTrashCollectedTime) 
    {
    	this._mallModel.lastTrashCollectedTime = lastTrashCollectedTime;
    },  
    
    initializeAllFloors: function()
    {
    	var allFloorsModelsArray = new GAMutableArray();
    	var allFloorsArray = this._mallModel.allFloors;
    	if(allFloorsArray) {
	    	var i = 0;
			for(var floorId in allFloorsArray) {
				//console.log("Zulu: In creation of FloorModel "+floorId);
				var floorModel = new MMFloorModel(allFloorsArray[floorId]);
				allFloorsModelsArray.insertObjectAtIndex(i, floorModel);
				i++;
			}
    	}
		
		return allFloorsModelsArray;
    },
    
    initializeAllStores: function()
    {
    	var allStoresModelsArray = new GAMutableArray();
    	var allStoresArray = this._mallModel.allStores;
    	if(allStoresArray) {
	    	var i = 0;
			for(var storeId in allStoresArray) {
				//console.log("Zulu: In creation of StoreModel "+storeId);
				var storeModel = new MMStoreModel(allStoresArray[storeId]);
				allStoresModelsArray.insertObjectAtIndex(i, storeModel);
				i++;
			}
    	}
		
		return allStoresModelsArray;
    },
    
    initializeAllElevators: function()
    {
    	var allElevatorsModelsArray = new GAMutableArray();
    	var allElevatorsArray = this._mallModel.allElevators;
    	if(allElevatorsArray) {
	    	var i = 0;
			for(var elevatorId in allElevatorsArray) 
			{
				var elevatorModel = new MMElevatorModel(allElevatorsArray[elevatorId]);
				allElevatorsModelsArray.insertObjectAtIndex(i, elevatorModel);
				i++;
			}
    	}
		
		return allElevatorsModelsArray;
    },
    
    addFloor: function()
    {
    	var floorModel = new MMFloorModel();
    	floorModel._floorObj.floorId = this._mallModel.floorId;
    	floorModel._floorObj.mallId = this._mallModel.mallId;
    	floorModel._floorObj.itemId = "2_1_1";
    	
    	this._allFloors.addObject(floorModel);
    		
    	this._mallModel.allFloors[floorModel._floorObj.floorId] = floorModel._floorObj;
    	this._mallModel.floorId++;
    		
    	this.updateModel();
    	
    	return floorModel;
    },
    
    addStore: function(itemId, position)
    {
    	if(itemId && position) 
    	{
	    	var storeModel = new MMStoreModel();
	    	storeModel.setStoreId(this._mallModel.storeId);
	    	storeModel.setMallId(this._mallModel.mallId);
	    	storeModel.setItemId(itemId);
	    	storeModel.setPosition(position);
	    	
	    	this._allStores.addObject(storeModel);
	    	
	    	this._mallModel.allStores[storeModel.getStoreId()] = storeModel._storeJson;
	    	this._mallModel.storeId++;
	    	
	    	this.updateModel();
	    	
	    	return storeModel;
    	}
    	return null;
    },
    
    addElevator: function(itemId)
    {
    	if(itemId) 
    	{
	    	var elevatorModel = new MMElevatorModel();
	    	elevatorModel.setElevatorId(this._mallModel.elevatorId);
	    	//elevatorModel.setMallId(this._mallModel.mallId);
	    	elevatorModel.setItemId(itemId);
	    	//elevatorModel.setPosition(position);
	    	
	    	this._allElevators.addObject(elevatorModel);
	    	
	    	this._mallModel.allElevators[elevatorModel.getElevatorId()] = elevatorModel._elevatorJson;
	    	this._mallModel.elevatorId++;
	    	
	    	this.updateModel();
	    	
	    	return elevatorModel;
    	}
    	return null;
    },
    
    updateModel: function()
    {
    	MMGameState.getCurrentUser().updateModel();
    },
    						   
	destroy: function()
	{
		this._mallModel = null;	
		if(this._allFloors) 
		{
			this._allFloors.destroyWithObjects();
			this._allFloors = null;
		}
	},
});